package map.search

import android.util.Log
import com.baidu.mapapi.model.LatLng
import com.baidu.mapapi.search.geocode.*
import com.baidu.mapapi.search.poi.*
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener
import com.baidu.mapapi.search.sug.SuggestionSearch
import com.baidu.mapapi.search.sug.SuggestionSearchOption
import map.MapBaseManager
import map.default.DefaultGetPoiSearchResultListener

class SearchManager : MapBaseManager() {

    companion object {
        private const val TAG = "SearchManager"
    }

    private var poiSearch: PoiSearch? = null

    private var suggestSearch: SuggestionSearch? = null

    private var geoSearch: GeoCoder? = null

    private var searchCall: DefaultGetPoiSearchResultListener? = null
    private var suggestCall: OnGetSuggestionResultListener? = null
    private var geoCall: OnGetGeoCoderResultListener? = null

    fun poiDetailSearch(uid: String, call: DefaultGetPoiSearchResultListener) {
        searchCall = call
        poiSearch?.searchPoiDetail(PoiDetailSearchOption().apply {
            this.poiUid(uid)
        })
    }

    /**
     * 建议搜索
     * */
    fun suggestSearch(keyWord: String, city: String = "重庆", call: OnGetSuggestionResultListener) {
        suggestCall = call
        suggestSearch?.requestSuggestion(SuggestionSearchOption().apply {
            mCity = city
            mKeyword = keyWord
        })
    }

    fun poiCitySearch(
        keyword: String, city: String = "重庆", pageNumber: Int = 0, pageCapacity: Int = 10,
        scope: Int = 1, poiFilter: PoiFilter? = null, isCityLimit: Boolean = false,
        call: DefaultGetPoiSearchResultListener
    ) {
        searchCall = call
        poiSearch?.searchInCity(PoiCitySearchOption().apply {
            this.mPageNum = pageNumber
            this.mPageCapacity = pageCapacity
            this.mKeyword = keyword
            //1 基本信息；2详情信息
            this.mScope = scope
            this.mCity = city
            //过滤信息
            this.mPoiFilter = poiFilter
            this.mIsCityLimit = isCityLimit
        })
    }

    fun poiNearSearch(
        location: LatLng,
        keyWord: String,
        radius: Int = 500,
        call: DefaultGetPoiSearchResultListener
    ) {
        searchCall = call
        poiSearch?.searchNearby(PoiNearbySearchOption().apply {
            //支持多个关键字并集检索，不同关键字间以$符号分隔，最多支持10个关键字检索。如:”银行$酒店”
            this.mKeyword = keyWord
            this.mLocation = location
            //默认500米
            this.mRadius = radius
            this.pageNum(0)
        })
    }

    fun genSearch(location: LatLng, call: OnGetGeoCoderResultListener) {
        geoCall = call

        geoSearch?.reverseGeoCode(
            ReverseGeoCodeOption().location(location).newVersion(1).radius(100)
        )
    }

    override fun onCreate() {
        poiSearch = PoiSearch.newInstance()
        poiSearch?.setOnGetPoiSearchResultListener(object : OnGetPoiSearchResultListener {
            override fun onGetPoiResult(p0: PoiResult?) {
                searchCall?.onGetPoiResult(p0)
            }

            override fun onGetPoiDetailResult(p0: PoiDetailResult?) {
                searchCall?.onGetPoiDetailResult(p0)
            }

            override fun onGetPoiDetailResult(p0: PoiDetailSearchResult?) {
                searchCall?.onGetPoiDetailResult(p0)
            }

            override fun onGetPoiIndoorResult(p0: PoiIndoorResult?) {
                searchCall?.onGetPoiIndoorResult(p0)
            }
        })

        suggestSearch = SuggestionSearch.newInstance().apply {
            setOnGetSuggestionResultListener {
                Log.d(TAG, "SuggestionSearch: $it")
                suggestCall?.onGetSuggestionResult(it)
            }
        }

        geoSearch = GeoCoder.newInstance().apply {
            setOnGetGeoCodeResultListener(object : OnGetGeoCoderResultListener {
                override fun onGetGeoCodeResult(p0: GeoCodeResult?) {
                    geoCall?.onGetGeoCodeResult(p0)
                }

                override fun onGetReverseGeoCodeResult(p0: ReverseGeoCodeResult?) {
                    geoCall?.onGetReverseGeoCodeResult(p0)
                }
            })
        }
    }

    override fun onDestroy() {
        poiSearch?.destroy()
        poiSearch = null

        suggestSearch?.destroy()
        suggestSearch = null

        geoSearch?.destroy()

        searchCall = null
        suggestCall = null
        geoCall = null
    }
}